<%@ page language="java" contentType="text/html; charset=UTF-8"
    pageEncoding="UTF-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>        

<script type="text/javascript">	
	$(document).ready(function(){
		//마우스 위치 저장할 변수
//		var x = null; 
//		var y = null; 
		//초기에 화면에 뿌려줄 내용
		$.ajax({
			type:"POST",
			url:"hopeSubjectList.do",
			dataType:"json",
			success:function(jsonData){	
 				//희망과목 가져와서 뿌리기
				$("#subject_hopesubject option").remove();
				op="<option value=''>--</option>";
				$("#subject_hopesubject").append(op);
 				$.each(jsonData.hopeSubjectList,function(index,hopesubjectlist){
 					op="<option value="+hopesubjectlist.code_code+">"+hopesubjectlist.code_name+"</option>";
					$("#subject_hopesubject").append(op); 
				});
			}//success
		});//ajax
		
		$("#subject_searchBtn").click(function(){
			var subject_hopesubject=$("#subject_hopesubject").val();
			var subject_name=$("#subject_name").val();
			
			if(subject_hopesubject=="" && name==""){
				alert("검색조건을 입력하세요.");
			}else{	//조건에 따른 검색
				$.ajax({
					type:"POST",
					url:"tabTeacherSubjectViewResult.do",
					data:"name="+subject_name+"&hopesubject="+subject_hopesubject,  //	data:$("#tradeForm").serialize(),
					dataType:"json",
					success:function(jsonData){
						//alert("검색조건 성공!!"+jsonData.list.length);
						if(jsonData.list.length>=1){	//검색결과 1명이상이면
							$("#subject_totalteacher").text("검색결과 총 선생님 수 : "+jsonData.list.length);
							$("#listbysubject tbody").text("");	//기존의 내용을 지운다.
	 		 				$.each(jsonData.list,function(index, tvo){
	 		 					member_table  = "<tr><td align='center'>"+tvo.name +"</td>"+
	 		 													"<td align='center'>"+tvo.hopesubject +"</td>"+
	 		 													"<td align='center'>"+tvo.hoperegion +"</td>"+
	 		 													"<td align='center'>"+tvo.gender +"</td>"+
	 		 													"<td align='center'>"+tvo.university +"</td>"+
	 		 													"<td align='center'>"+tvo.availablestudent +"</td>"+
	 		 													"<td align='center'>"+tvo.availableday +"</td></tr>";			
	 							$("#subject_memberlist_Tbody").append(member_table);				
							});
							$("#subject_hopesubject").val("");	//검색완료 후 초기화(과목셀렉트박스)
							$("#subject_name").val("");		//검색완료 후 초기화(이름)
						}else{
							alert("검색조건에 맞는 학생이 없습니다.");	
						}
					}
				});	//ajax			
			}	// if
		});	//click

		if($("input[name=membertype]").val()=="s"){
			 var dataString = "teacherid="+$("a[id=teacherName]").next().val()
				+"&studentid="+$("input[name=id]").val()
				+"&listtype=s";
			 $( "#dialog_teacherInfo" ).dialog({
			        autoOpen: false,
			        show: { effect: "blind", duration: 100 },
			        hide: {effect: "explode", duration: 100},
			        buttons: {
			        	"찜하기": function(){
			        		$.ajax({
			        			type:"POST",
			    				url:"registerSTSelectedList.do",
			    				data:dataString,  
			    				dataType:"json",
			    				success:function(jsonData){
			    					if(jsonData=="true"){
			    						$( "#dialog_teacherInfo" ).dialog({
			    					        autoOpen: false,
			    					        show: { effect: "blind", duration: 100 },
			    					        hide: {effect: "explode", duration: 100},
			    					        buttons: {
			    					        	"찜리스트 보기": function(){
			    					        		location.href="showSelectedList.do?id="+$("input[name=id]").val()+
			    					        			"&membertype="+$("input[name=membertype]").val();
			    					        	}
			    					        }
			    					 });
			    					}
			    				}
			        		});
			        	}
			        }
//			        position: [x,y+10]
			 });
		 }else{
			 $( "#dialog_teacherInfo" ).dialog({
			        autoOpen: false,
			        show: { effect: "blind", duration: 100 },
			        hide: {effect: "explode", duration: 100}
			 });
		 }
		
		//선생님 이름 클릭되면
 		$("a[id=teacherName]").click(function(e){ 
//			alert($(this).next().val());
// 			x = e.clientX; y = e.clientY;	
			var teacherId=$(this).next().val();
			var information="";
			$.ajax({
				type:"POST",
				url:"getTeacherInfobyId.do",
				data:"teacherId="+teacherId,  
				dataType:"json",
				success:function(jsonData){

					$("#p_teacherIdInfo").text("");	//기존의 내용을 지운다.
					var string="<img src='${initParam.root }upload/"+jsonData.savefilename+"' width='150'><br>";
					if(jsonData.savefilename==null){
						string="<img src='${initParam.root }img/default.jpg' width='150'><br>"
					}
					information  = string+
										  "<font size='2'>이름 : "+jsonData.name+"</font><br>"+
										  "<font size='2'>과외지역 : "+jsonData.hoperegion+"</font><br>"+
										  "<font size='2'>과외과목 : "+jsonData.hopesubject+"</font><br>"+
										  "<font size='2'>가능학년 : "+jsonData.availablestudent+"</font><br>"+
										  "<font size='2'>전화번호 : "+jsonData.tel+"</font><br>"+
										  "<font size='2'>이메일 : "+jsonData.email+"</font><br>";
										  	 													
 					$("#dialog_teacherInfo").html(information);
 					$( "#dialog_teacherInfo").dialog( "open" );
				}	//success
			});	//ajax 			
		});		
		
	});
</script>

<input type="hidden" name="id" value="${sessionScope.mvo.id }">
<input type="hidden" name="membertype" value="${sessionScope.mvo.membertype }">


<form id=subject_searchForm>
<table width="560"  cellpadding="2" cellspacing="0"><tr><td>
	<table  >
		<tr>
			<td>희망과목</td><td><select id="subject_hopesubject"></select></td>	
			<td>이름</td><td><input type="text" id="subject_name"></td>
			<td align="right"><input type="button" id="subject_searchBtn" value="검색"> </td>
		</tr>
	</table>
</td></tr></table>	
</form>
<table width="560" cellpadding="2" cellspacing="0"><tr align="right"><td>
<div id="subject_totalteacher">총 선생님 수 : ${requestScope.listVO.pagingBean.totalContent}</div>
</td></tr></table>	
<table id="listbysubject" width="560"  border="1" cellpadding="2" cellspacing="0">
	<thead>
		<tr><td width="10%" align="center">이름</td>
			 <td width="15%" align="center">과외과목</td>		
			 <td width="20%" align="center">희망지역</td>
			 <td width="8%" align="center">성별</td>
		 	 <td width="15%" align="center">출신학교</td>			
			 <td width="18%" align="center">대상학생</td>
			 <td width="10%" align="center">가능요일</td>		
		</tr>
	</thead>
	<tbody id="subject_memberlist_Tbody">
			<c:forEach var="tvo" items="${requestScope.listVO.list}">		
			<tr>
			   <td align='center'><a href="#" id="teacherName">${tvo.name }</a>
			    <input type="hidden" id="hidden_tvo" value="${tvo.id }"></td>
			    <td align="center">${tvo.hopesubject}</td>
			    <td align="center">${tvo.hoperegion}</td>
			    <td align="center">${tvo.gender}</td>
			    <td align="center">${tvo.university}</td>		
			    <td align="center">${tvo.availablestudent}</td>							
				<td align="center">${tvo.availableday}</td>				
			</tr>
			</c:forEach>	
	</tbody>	
</table><br>

<div id="dialog_teacherInfo" title="개인정보">
 	 <p id="p_teacherInfo">개인정보</p>
</div>

<table width="560"  cellpadding="2" cellspacing="0"><tr align="center"><td>
<c:set var="pb" value="${requestScope.listVO.pagingBean}"></c:set>
<c:if test="${pb.previousPageGroup}">
	<a href="tabTeacherSubjectView.do?pageNo=${pb.startPageOfPageGroup-1 }">◀</a>
</c:if>	

<c:forEach var="pageNo" begin="${pb.startPageOfPageGroup }" 
										end="${pb.endPageOfPageGroup }">
<c:choose>
<c:when test="${pb.nowPage !=pageNo }" >
	<a href="tabTeacherSubjectView.do?pageNo=${pageNo }" >${pageNo }</a>
</c:when>
<c:otherwise>
	${pageNo} 
</c:otherwise>
</c:choose>
</c:forEach>

<c:if test="${pb.nextPageGroup}">
	<a href="tabTeacherSubjectView.do?pageNo=${pb.endPageOfPageGroup+1 }">▶</a>
</c:if>
</td></tr></table>	
<br>